#!/bin/bash

# 环境工程咨询系统一键部署脚本
# 服务器: 8.137.120.76 (gzqhst.com)
# 项目: https://gitee.com/lizaoboss/gzqhsz.git

set -e

echo "🚀 开始一键部署环境工程咨询系统..."

# 配置国内镜像源
echo "🔧 配置国内镜像源..."

# 备份原始sources.list
cp /etc/apt/sources.list /etc/apt/sources.list.backup

# 配置阿里云镜像源
cat > /etc/apt/sources.list << 'EOF'
# 阿里云镜像源
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
EOF

# 更新系统
echo "📦 更新系统..."
apt update && apt upgrade -y

# 安装必要软件
echo "🔧 安装必要软件..."
apt install -y python3 python3-pip python3-venv nodejs npm nginx git curl ufw

# 配置npm国内镜像源
echo "🔧 配置npm国内镜像源..."
npm config set registry https://registry.npmmirror.com

# 安装PM2
echo "📋 安装PM2..."
npm install -g pm2

# 克隆项目
echo "📥 克隆项目..."
cd /var/www
if [ -d "environmental-consulting" ]; then
    rm -rf environmental-consulting
fi
git clone https://gitee.com/lizaoboss/gzqhsz.git environmental-consulting
cd environmental-consulting

# 配置pip国内镜像源
echo "🔧 配置pip国内镜像源..."
mkdir -p ~/.pip
cat > ~/.pip/pip.conf << 'EOF'
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
EOF

# 运行项目部署脚本
echo "🏗️ 运行项目部署..."
chmod +x deploy.sh
./deploy.sh production

# 配置环境变量
echo "⚙️ 配置环境变量..."
cd backend
cat > .env << 'EOF'
# 数据库配置
DATABASE_URL=sqlite:///./environmental_consulting.db

# JWT密钥
SECRET_KEY=gzqhsz-environmental-consulting-super-secret-key-2024

# 应用配置
DEBUG=false
HOST=0.0.0.0
PORT=8001

# 前端URL
FRONTEND_URL=https://gzqhst.com
EOF

cd ..

# 配置Nginx
echo "🌐 配置Nginx..."
sed -i 's/your-domain.com/gzqhst.com/g' nginx.conf
sed -i 's|/path/to/your/project|/var/www/environmental-consulting|g' nginx.conf

cp nginx.conf /etc/nginx/sites-available/environmental-consulting
ln -sf /etc/nginx/sites-available/environmental-consulting /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default

nginx -t
systemctl restart nginx
systemctl enable nginx

# 配置PM2
echo "🔄 配置PM2..."
sed -i 's|/path/to/your/project|/var/www/environmental-consulting|g' ecosystem.config.js
mkdir -p logs

pm2 start ecosystem.config.js
pm2 save
pm2 startup --hp /root

# 配置防火墙
echo "🔒 配置防火墙..."
ufw allow 22
ufw allow 80
ufw allow 443
ufw --force enable

echo "✅ 部署完成！"
echo ""
echo "🌐 访问地址:"
echo "   - 网站首页: https://gzqhst.com"
echo "   - API文档: https://gzqhst.com/docs"
echo ""
echo "🔑 默认管理员账户:"
echo "   - 用户名: admin"
echo "   - 密码: admin123"
echo ""
echo "🔧 管理命令:"
echo "   - 查看服务: pm2 status"
echo "   - 重启服务: pm2 restart environmental-api"
echo "   - 查看日志: pm2 logs environmental-api"
echo ""
echo "🎉 祝您使用愉快！"
